Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find the Largest Power of 2 That Divides N!

Basic Acceptance 96.85% Points 10

You are given a non-negative integer N. Your task is to return the largest exponent e such that 2e divides N! (N factorial).

To find this exponent efficiently, count the number of factors of 2 contributed by all multiples of 2, 4, 8, and higher powers of 2 that do not exceed N. Add these counts to obtain the final result.

Examples
Example 1

Input: N = 10

Output: 8

Explanation: The exponent is calculated as 10 / 2 + 10 / 4 + 10 / 8 = 5 + 2 + 1 = 8.

Example 2

Input: N = 5

Output: 3

Explanation: The exponent is calculated as 5 / 2 + 5 / 4 = 2 + 1 = 3.

Example 3

Input: N = 1

Output: 0

Explanation: 1! = 1 is not divisible by 2, so the largest possible exponent is 0.

Constraints
  • 0 <= N <= 109
Topics
Introduction Data Structures Basics Basics
Companies
Apple DE Shaw
Added 05 Nov 2026 96.85% acceptance
Ln 1, Col 1Sign in to save your code
5
3